package com.blindBox.front.controller.paper;

import com.blindBox.front.commonResult.MsgBackToFront;
import com.blindBox.front.dao_pojo.Paper;
import com.blindBox.front.handler.paperHandler.AbstractPaperHandler;
import com.blindBox.front.handler.paperHandler.paperHandlerImpl.PaperHandler;
import com.blindBox.front.parse.PaperParse;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/*
* 处理纸条投放与拿取的控制类
* */
@RestController
public class PaperController
{
    @Resource(name="paperHandler")
    private AbstractPaperHandler paperHandler;


   //纸条放入盲盒，判断性别，如果是男性，只能放入男性盲盒，如果是女性，只能放入女性盲盒中
   @PostMapping("/paper_into_box") //需要的数据contactdata，selfinfo,userid,del,selfImgs
    public synchronized MsgBackToFront paperIntoBox(Paper paper)
   {
       //如果没有上传照片，就设置字段为null，用默认值代替
       if(paper.getSelfImgs().equals("NO_FILE_UPLOAD"))
       {
           paper.setSelfImgs(null);
       }
     //保存用户投掷的纸条到盲盒中
      return paperHandler.paperPutIn(paper);
   }

   //处理纸条里面的多图片
    @PostMapping("/paper_file_handle")
    public synchronized MsgBackToFront paperFileHandle(@RequestParam("self_img") MultipartFile[] imgs,@RequestParam("u_id")Integer u_id)
    {
       PaperHandler ph=(PaperHandler) paperHandler;
        return ph.handleSelfImgs(imgs,u_id);
    }

    //从盲盒中拿取一个纸条,分男女
    @PostMapping("/paper_out_box")
    public synchronized MsgBackToFront paperTakeOutOfBox(@RequestParam("sex")Integer sex//当前用户的id
    ,@RequestParam("u_id")Integer u_id)
     {
         //随机抽取一张纸条
         return paperHandler.paperTakeOut(sex,u_id);
     }

     //返回当前用户拥有的所有纸条
    @PostMapping("/get_user_all_papers")
    public synchronized MsgBackToFront getUserAllPapers(@RequestParam("u_id")Integer u_id)
    {
        PaperHandler p=(PaperHandler)paperHandler;
        return p.getUserAllPapers(u_id);
    }

    //返回当前抽取到的所有纸条
    @PostMapping("/get_user_get_papers")
    public synchronized MsgBackToFront displayUserGetPapers(@RequestParam("u_id")Integer u_id)
    {
        PaperHandler p=(PaperHandler)paperHandler;
        return p.getUsersGetPapers(u_id);
    }

    //返回当前用户发出去并被别人抽取到的纸条
    @PostMapping("/get_paper_chosed_by_other")
    public synchronized MsgBackToFront getPaperChosedByOther(@RequestParam("u_id")Integer u_id)
    {
        PaperHandler p=(PaperHandler)paperHandler;
        return p.getUserPostAndChoseByOtherPaper(u_id);
    }

    //删除纸条
    @PostMapping("/del_paper_by_id")
    public  synchronized MsgBackToFront delPaper(@RequestParam("p_id")Integer p_id)
    {
        return paperHandler.delPaper(p_id);
    }
}
